def get_prefix_dict(f_name):
    lfreq = {}
    ltotal = 0
    f = open(f_name)
    for lineno, line in enumerate(f, 1):

        line = line.strip()
        words_freq = line.split(' ')
        word, freq = words_freq[0],words_freq[1]
        freq = float(freq)
        lfreq[word] = freq
        ltotal += freq
        for ch in range(len(word)):
            wfrag = word[:ch + 1]
            if wfrag not in lfreq:
                lfreq[wfrag] = 0

    f.close()
    return lfreq, ltotal
if __name__=="__main__":
    freq, total = get_prefix_dict("./dict.txt")
    print("生成前缀词典的大小为{}。".format(freq))
